package cn.b.sky.mes.qm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.qm.model.Defect;
import cn.b.sky.mes.qm.model.DefectAttributeValue;
import cn.b.sky.mes.qm.model.condition.DefectCondition;
import cn.b.sky.mes.qm.service.DefectService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.mes.qm.dao.DefectDao;

@Service("defectService")
public class DefectServiceImpl implements DefectService {
  
  private static final Logger LOG = LoggerFactory.getLogger(DefectServiceImpl.class);

  @Resource
  DefectDao defectDao;

  @Override
  public List<Defect> selectDefectListByCondition(DefectCondition condition) {
    if (condition == null) {
      condition = new DefectCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return defectDao.selectDefectListByCondition(condition);
  }

  @Override
  public int selectDefectCountByCondition(DefectCondition condition) {
    if (condition == null) {
      condition = new DefectCondition();
    }
    return defectDao.selectDefectCountByCondition(condition);
  }

  @Override
  public Defect getDefect(String defectCode, String siteCode) {
    if (StringUtils.isBlank(defectCode) || StringUtils.isBlank(siteCode)) {
      return null;
    }
    return defectDao.getDefect(defectCode, siteCode);
  }

  @Override
  public boolean insertDefect(Defect defect) {
    if (StringUtils.isBlank(defect.getDefectCode()) || StringUtils.isBlank(defect.getSiteCode())) {
      return false;
    }
    defectDao.insertDefect(defect);
    List<DefectAttributeValue> values = defect.getDefectAttributeValue();
    for (DefectAttributeValue value : values) {
      if (!StringUtils.isBlank(value.getAttributeValue())) {
        value.setDefectCode(defect.getDefectCode());
        value.setSiteCode(defect.getSiteCode());
        defectDao.insertDefectAttributeValue(value);
      }
    }
    return true;

  }

  @Override
  public boolean updateDefect(Defect defect) {
    if (StringUtils.isBlank(defect.getDefectCode()) || StringUtils.isBlank(defect.getSiteCode())) {
      return false;
    }
    defectDao.updateDefect(defect);
    List<DefectAttributeValue> values = defect.getDefectAttributeValue();
    if (values.size() > 0) {
      defectDao.deleteDefectAttributeValue(defect.getSiteCode(), defect.getDefectCode());
      for (DefectAttributeValue value : values) {
        if (!StringUtils.isBlank(value.getAttributeValue())) {
          value.setDefectCode(defect.getDefectCode());
          value.setSiteCode(defect.getSiteCode());
          defectDao.insertDefectAttributeValue(value);
        }
      }
    }
    return true;
  }

  @Override
  public BatchJobReport deleteDefect(String siteCode, String defectCode) {
    String languageCode = SecurityContextUtil.getLanguageCode();
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(defectCode)) {
      return new BatchJobReport("", "siteCode or defectCode is null", false, languageCode, SecurityContextUtil.getSiteCode());
    }
    String message = defectCode;
    boolean success = true;
    String cause = "";

    try {
      defectDao.deleteDefectAttributeValue(siteCode, defectCode);
      defectDao.deleteDefect(siteCode, defectCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(errorMsg)) {
        // 外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success, languageCode, SecurityContextUtil.getSiteCode());
  }

  @Override
  public List<Defect> selectDefectListSuggestByCondition(DefectCondition condition) {
    if (condition == null) {
      condition = new DefectCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return defectDao.selectDefectListSuggestByCondition(condition);
  }

  @Override
  public Defect selectDefectOnly(String defectCode, String siteCode) {
    return defectDao.getDefect(defectCode, siteCode);
  }

}
